package cn.softeem.tushu.Dao.Impl;

import cn.softeem.tushu.Dao.CartitemDao;
import cn.softeem.tushu.po.Book;
import cn.softeem.tushu.po.Cartitem;
import cn.softeem.tushu.po.User;
import cn.softeem.tushu.utils.BeanUtil;
import cn.softeem.tushu.utils.TxQueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CartitemDaoImpl implements CartitemDao {
    TxQueryRunner tqr = new TxQueryRunner();

    private Cartitem toBean(Map<String,Object> map) {
       Cartitem cartitem =BeanUtil.toBeans(map,Cartitem.class);
       Book book =BeanUtil.toBeans(map,Book.class);
        User user=BeanUtil.toBeans(map,User.class);
        cartitem.setBook(book);
        cartitem.setUser(user);
        return  cartitem;
    }
    private List<Cartitem> toBeanList(List<Map<String,Object>> mapList) {
        List<Cartitem> cartItemList = new ArrayList<Cartitem>();
        for(Map<String,Object> map : mapList) {
            cartItemList.add(toBean(map));
        }
        return cartItemList;
    }
    @Override
    public List<Cartitem> myCart(String uid) throws SQLException {
        String sql = "select * from t_cartItem ci, t_book b where ci.bid=b.bid and uid=? order by ci.orderBy";
        List<Map<String, Object>> mapList =tqr.query(sql,new MapListHandler(),uid);
        return toBeanList(mapList);
    }

    @Override
    public void addCart(Cartitem cartitem) throws SQLException {
        tqr.update("insert into t_cartitem(cartItemId,quantity,bid,uid)values (?,?,?,?)",cartitem.getCartItemId(),cartitem.getQuantity(),cartitem.getBook().getBid(),cartitem.getUser().getUid());

    }

    @Override
    public Cartitem findByUserAndBook(String uid, String bid) throws SQLException {

        return tqr.query("select * from t_cartitem where uid=? and bid=?",new BeanHandler<>(Cartitem.class),uid,bid);
    }

    @Override
    public void updateQuantity(String cartitemId, int quantity) throws SQLException {
        tqr.update("update t_cartitem set quantity = ? where cartitemId=?",quantity,cartitemId);
    }
    private String getSql(int len) {
        StringBuilder sb = new StringBuilder("cartItemId in(");
        for(int i = 0; i < len; i++) {
            sb.append("?");
            if(i < len - 1) {
                sb.append(",");
            }
        }
        sb.append(")");
        return sb.toString();
    }

    @Override
    public void delete(String cartitemIds) throws SQLException {
        Object[] params = cartitemIds.split(",");//必须是Object[]类型，而不能是String[]类型！
        String sql="delete from t_cartitem where "+getSql(params.length);
        tqr.update(sql,params);
    }

    @Override
    public List<Cartitem> loadCartItems(String cartItemIds) throws SQLException {
        Object[] cartArray = cartItemIds.split(",");
        String sql="select * from t_cartitem c, t_book b where c.bid=b.bid and "+getSql(cartArray.length);
        List<Map<String, Object>> list = tqr.query(sql, new MapListHandler(), cartArray);
        return toBeanList(list);
    }

}
